package ttsolver.variables;

import ttsolver.input.Node;
import ttsolver.input.Train;

public class Xarrive extends DefaultVariable {

	private Train train;
	private Node destination;
	// private Node arrive;
	private int time;

	public Xarrive() {
		super(VariableType.XARRIVE);
	}

	public Xarrive(Train train, Node destination, int time) {
		super(VariableType.XARRIVE);
		this.train = train;
		this.destination = destination;
		// this.arrive = arrive;
		this.time = time;
	}

	public Train getTrain() {
		return train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Node getDestination() {
		return destination;
	}

	public void setDestination(Node destination) {
		this.destination = destination;
	}

	// public Node getArrive() {
	// return arrive;
	// }
	//
	// public void setArrive(Node arrive) {
	// this.arrive = arrive;
	// }

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((train == null) ? 0 : train.getId().intValue());
		result = prime * result
				+ ((destination == null) ? 0 : destination.getId().intValue());
		// result = prime * result + ((arrive == null) ? 0 :
		// arrive.getId().intValue());
		result = prime * result + time;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;

		Xarrive other = (Xarrive) obj;

		if (train == null) {
			if (other.train != null)
				return false;
		} else if (!train.equals(other.train)) {
			return false;
		}

		if (destination == null) {
			if (other.destination != null)
				return false;
		} else if (!destination.equals(other.destination)) {
			return false;
		}

		// if(arrive == null){
		// if(other.arrive != null)
		// return false;
		// }else if(!arrive.equals(other.arrive)){
		// return false;
		// }

		if (time != other.time) {
			return false;
		}

		return true;
	}

	@Override
	public int compareTo(Object obj) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		Xarrive other = (Xarrive) obj;

		if (this == other)
			return EQUAL;

		if (this.train != null && other.train != null) {
			if (this.train.getId() < other.train.getId())
				return BEFORE;
			if (this.train.getId() > other.train.getId())
				return AFTER;
		}

		if (this.destination != null && other.destination != null) {
			if (this.destination.getId() < other.destination.getId())
				return BEFORE;
			if (this.destination.getId() > other.destination.getId())
				return AFTER;
		}

		// if(this.arrive != null && other.arrive != null){
		// if(this.arrive.getId() < other.arrive.getId())
		// return BEFORE;
		// if(this.arrive.getId() > other.arrive.getId())
		// return AFTER;
		// }

		if (this.time < other.time)
			return BEFORE;
		if (this.time > other.time)
			return AFTER;

		return EQUAL;
	}

	@Override
	public String toString() {
		if (!printVar)
			return "";

		String s = (train != null) ? String.valueOf(train.getId()) : "";
		s += (destination != null) ? "," + String.valueOf(destination.getId())
				: "";
		// s += (arrive != null) ? "," + String.valueOf(arrive.getId()) : "";
		s += "," + String.valueOf(time);
		return "Xarrive_(" + s + ")";
	}

}
